<?php
    session_start();

    if (!isset($_SESSION['is_logged_in'])) {
        header("Location:../index.php");
        die();
    }
    
    ini_set('display_errors',"1");
    include("conexion.php");
    
    $operacion = $_POST["operacion"];
    $NumeUsua = $_POST["NumeUsua"];
    $NombUsua = $_POST["NombUsua"];
	$NombPass = $_POST["NombPass"];
	$NumeCarg = $_POST["NumeCarg"];
    
    
    $conn = new mysqli($dbhost, $dbuser, $dbpass, $db) or die("Problemas al conectar con la BD");
    
    switch ($operacion) {
        case 0:
            $strSQL = "SELECT COALESCE(MAX(NumeUsua), 0) + 1 NumeUsua FROM usuarios";
            $tabla = $conn->query($strSQL);
            $fila = $tabla->fetch_array();
            
            $strSQL = "INSERT INTO usuarios(NumeUsua, NombUsua, NombPass, NumeCarg)";
            $strSQL.= " VALUES({$fila["NumeUsua"]}, '{$NombUsua}', '{$NombPass}', {$NumeCarg})";

            if (!$conn->query($strSQL))
                echo "Fall&oacute; INSERT:<br />(" . $conn->errno . ") " . $conn->error . "<br />" . $strSQL;
            else
                echo "INSERT Correcto!";                
            
            $tabla->free();
            break;
        
        case 1:
            $strSQL = "UPDATE usuarios";
            $strSQL.= " SET NombUsua = '" . $NombUsua . "'";
           	$strSQL.= ", NombPass = '" . $NombPass . "'";
            $strSQL.= ", NumeCarg = " . $NumeCarg;
            $strSQL.= " WHERE NumeUsua = " . $NumeUsua;
            
            if (!$conn->query($strSQL))
                echo "Fall&oacute; UPDATE:<br />(" . $conn->errno . ") " . $conn->error . "<br />" . $strSQL;
            else
                echo "UPDATE Correcto!";
            
            break;
            
        case 2:
            $strSQL = "DELETE FROM usuarios WHERE NumeUsua = " . $NumeUsua;
            if (!$conn->query($strSQL))
                echo "Fall&oacute; DELETE:<br />(" . $conn->errno . ") " . $conn->error . "<br />" . $strSQL;
            else
                echo "DELETE Correcto!";
            
            break;
    }
    $conn->close();
?>